/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.zhaojiu.service.impl;

import me.zhengjie.modules.zhaojiu.domain.Teacher;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.zhaojiu.repository.TeacherRepository;
import me.zhengjie.modules.zhaojiu.service.TeacherService;
import me.zhengjie.modules.zhaojiu.service.dto.TeacherDto;
import me.zhengjie.modules.zhaojiu.service.dto.TeacherQueryCriteria;
import me.zhengjie.modules.zhaojiu.service.mapstruct.TeacherMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author Jiayin Wei
* @date 2021-12-09
**/
@Service
@RequiredArgsConstructor
public class TeacherServiceImpl implements TeacherService {

    private final TeacherRepository teacherRepository;
    private final TeacherMapper teacherMapper;

    @Override
    public Map<String,Object> queryAll(TeacherQueryCriteria criteria, Pageable pageable){
        Page<Teacher> page = teacherRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(teacherMapper::toDto));
    }

    @Override
    public List<TeacherDto> queryAll(TeacherQueryCriteria criteria){
        return teacherMapper.toDto(teacherRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public TeacherDto findById(Long teacherId) {
        Teacher teacher = teacherRepository.findById(teacherId).orElseGet(Teacher::new);
        ValidationUtil.isNull(teacher.getTeacherId(),"Teacher","teacherId",teacherId);
        return teacherMapper.toDto(teacher);
    }

    @Override
    @Transactional
    public TeacherDto findByUserId(Long userId){
        Teacher teacher = teacherRepository.findByUserId(userId);
        return teacherMapper.toDto(teacher);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TeacherDto create(Teacher resources) {
        return teacherMapper.toDto(teacherRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Teacher resources) {
        Teacher teacher = teacherRepository.findById(resources.getTeacherId()).orElseGet(Teacher::new);
        ValidationUtil.isNull( teacher.getTeacherId(),"Teacher","id",resources.getTeacherId());
        teacher.copy(resources);
        teacherRepository.save(teacher);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long teacherId : ids) {
            teacherRepository.deleteById(teacherId);
        }
    }

    @Override
    public void download(List<TeacherDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TeacherDto teacher : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("姓名", teacher.getName());
            map.put("用户ID", teacher.getUserId());
            map.put("性别ID", teacher.getGenderId());
            map.put("民族ID", teacher.getNationId());
            map.put("照片真实路径", teacher.getPhotoUrl());
            map.put("工作单位", teacher.getWorkUnit());
            map.put("出生年月", teacher.getBirthDate());
            map.put("政治面貌ID", teacher.getPoliticalId());
            map.put("学历层次ID", teacher.getEducationalId());
            map.put("学位", teacher.getDegree());
            map.put("专业", teacher.getMajor());
            map.put("联系电话", teacher.getPhone());
            map.put("身份证号", teacher.getIdCardNo());
            map.put("银行卡号", teacher.getBankNo());
            map.put("现任职务", teacher.getJob());
            map.put("专业技术职称", teacher.getPostitional());
            map.put("可承担教学科目", teacher.getSubjects());
            map.put("备注", teacher.getRemark());
            map.put("状态", teacher.getEnabled());
            map.put("创建者", teacher.getCreateBy());
            map.put("更新者", teacher.getUpdateBy());
            map.put("创建日期", teacher.getCreateTime());
            map.put("更新时间", teacher.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
